/*
 * COPYRIGHT:	2010 Christopher Smith, licenced under the GNU GPL v2; see COPYING in the top level directory
 * PROJECT:		sdedit
 * FILE:		ntds ldap opts.cpp
 * PURPOSE:		NTDS over LDAP user options
 *				
 */

namespace Ldap
{

static const UCHAR LDAP_OPTNUM_SHIFT = 16;

SDE_ENUM_VAL AuthEnumVals[] = 
{
	{L"NEGOTIATE", LDAP_AUTH_NEGOTIATE},
	{L"SIMPLE", LDAP_AUTH_SIMPLE},
	{L"SASL", LDAP_AUTH_SASL},
	{L"SICILY", LDAP_AUTH_SICILY},
	{L"MSN", LDAP_AUTH_MSN},
	{L"NTLM", LDAP_AUTH_NTLM},
	{L"DPA", LDAP_AUTH_DPA},
	{L"SSPI", LDAP_AUTH_SSPI},
	{L"DIGEST", LDAP_AUTH_DIGEST},
	{L"EXTERNAL", LDAP_AUTH_EXTERNAL},
};

SDE_ENUM AuthEnum = 
{
	RTL_NUMBER_OF(AuthEnumVals),
	LDAP_AUTH_NEGOTIATE,
	AuthEnumVals
};

//ISC_REQ_HTTP mentioned in InitializeSecurityContext, but no such constant is defined in the SDK
#ifndef ISC_REQ_HTTP
#define ISC_REQ_HTTP 0x10000000
#endif

SDE_ENUM_VAL SspiFlagEnumVals[] =
{
	{L"DELEGATE", ISC_REQ_DELEGATE},
	{L"MUTUAL_AUTH", ISC_REQ_MUTUAL_AUTH},
	{L"REPLAY_DETECT", ISC_REQ_REPLAY_DETECT},
	{L"SEQUENCE_DETECT", ISC_REQ_SEQUENCE_DETECT},
	{L"CONFIDENTIALITY", ISC_REQ_CONFIDENTIALITY},
	{L"USE_SESSION_KEY", ISC_REQ_USE_SESSION_KEY},
	{L"PROMPT_FOR_CREDS", ISC_REQ_PROMPT_FOR_CREDS},
	{L"USE_SUPPLIED_CREDS", ISC_REQ_USE_SUPPLIED_CREDS},
	{L"USE_DCE_STYLE", ISC_REQ_USE_DCE_STYLE},
	{L"DATAGRAM", ISC_REQ_DATAGRAM},
	{L"CONNECTION", ISC_REQ_CONNECTION},
	{L"CALL_LEVEL", ISC_REQ_CALL_LEVEL},
	{L"FRAGMENT_SUPPLIED", ISC_REQ_FRAGMENT_SUPPLIED},
	{L"EXTENDED_ERROR", ISC_REQ_EXTENDED_ERROR},
	{L"STREAM", ISC_REQ_STREAM},
	{L"INTEGRITY", ISC_REQ_INTEGRITY},
	{L"IDENTIFY", ISC_REQ_IDENTIFY},
	{L"NULL_SESSION", ISC_REQ_NULL_SESSION},
	{L"MANUAL_CRED_VALIDATION", ISC_REQ_MANUAL_CRED_VALIDATION},
	{L"FRAGMENT_TO_FIT", ISC_REQ_FRAGMENT_TO_FIT},
	{L"HTTP", ISC_REQ_HTTP}, 
};

SDE_ENUM SspiFlagEnum = 
{
	RTL_NUMBER_OF(SspiFlagEnumVals),
	0,
	SspiFlagEnumVals
};

SDE_ENUM_VAL DerefEnumVals[] =
{
	{L"NEVER", LDAP_DEREF_NEVER},
	{L"SEARCHING", LDAP_DEREF_SEARCHING},
	{L"FINDING", LDAP_DEREF_FINDING},
	{L"ALWAYS", LDAP_DEREF_ALWAYS},
};

SDE_ENUM DerefEnum = 
{
	RTL_NUMBER_OF(DerefEnumVals),
	0,
	DerefEnumVals
};

SDE_ENUM_VAL ReferralsModeEnumVals[] =
{
	{L"OFF", (ULONG_PTR)LDAP_OPT_OFF},
	{L"ON", (ULONG_PTR)LDAP_OPT_ON},
	{L"CHASE_SUBORDINATE", (ULONG_PTR)LDAP_CHASE_SUBORDINATE_REFERRALS},
	{L"CHASE_EXTERNAL", (ULONG_PTR)LDAP_CHASE_EXTERNAL_REFERRALS},
};

SDE_ENUM ReferralsModeEnum = 
{
	RTL_NUMBER_OF(ReferralsModeEnumVals),
	0,
	ReferralsModeEnumVals
};

SDE_ENUM DsnameFlagsEnum = 
{
	RTL_NUMBER_OF(Netapi::DsnameFlagsEnumVals),
	DS_DIRECTORY_SERVICE_REQUIRED | DS_WRITABLE_REQUIRED,
	Netapi::DsnameFlagsEnumVals
};

SDE_ENUM DsGetnameFlagsEnum = 
{
	RTL_NUMBER_OF(Netapi::DsnameFlagsEnumVals),
	DS_DIRECTORY_SERVICE_REQUIRED,
	Netapi::DsnameFlagsEnumVals
};

enum OptionIndex //synch with Options
{
	OiHost,
	OiDomain,
	OiDomainDc,
	OiForest,
	OiForestDc,
	OiAuth,
	OiWho,
	OiDsnameopts,
	OiLdapOpts
};

//those options with no specified type default to 0 or SptOther, invalid to specify (included here for completeness)
SDE_OPT StaticLdapOptions[] = 
{
	{L"HOST", 0, SptString},
	{L"DOMAIN", 0, SptString},
	{L"DOMAIN_DC", 0, SptString},
	{L"FOREST", 0, SptString},
	{L"FOREST_DC", 0, SptString},
	{L"AUTHMODE", SDE_OPT_SET, SptEnum, &AuthEnum},
	{L"WHO", 0, SptString},
	{L"DSNAME_FLAGS", SDE_OPT_SET, SptMultiEnumOr, &DsnameFlagsEnum},
	//{L"API_INFO", 0x00 << LDAP_OPTNUM_SHIFT},
	{L"API_FEATURE_INFO", 0x15 << LDAP_OPTNUM_SHIFT},
	{L"AREC_EXCLUSIVE", 0x98 << LDAP_OPTNUM_SHIFT | SDE_OPT_SET, SptBool, LDAP_OPT_ON},
	{L"AUTO_RECONNECT", 0x91 << LDAP_OPTNUM_SHIFT, SptBool},
	{L"CACHE_ENABLE", 0x0F << LDAP_OPTNUM_SHIFT},
	{L"CACHE_FN_PTRS", 0x0D << LDAP_OPTNUM_SHIFT},
	{L"CACHE_STRATEGY", 0x0E << LDAP_OPTNUM_SHIFT},
	{L"CLIENT_CERTIFICATE", 0x80 << LDAP_OPTNUM_SHIFT},
	{L"DEREF", 0x02 << LDAP_OPTNUM_SHIFT | SDE_OPT_SET, SptEnum, &DerefEnum},
	{L"DESC", 0x01 << LDAP_OPTNUM_SHIFT},
	{L"DNSDOMAIN_NAME", 0x3B << LDAP_OPTNUM_SHIFT, SptString},
	{L"ENCRYPT", 0x96 << LDAP_OPTNUM_SHIFT | SDE_OPT_SET, SptBool, LDAP_OPT_ON},
	{L"ERROR_NUMBER", 0x31 << LDAP_OPTNUM_SHIFT, SptInt32},
	{L"ERROR_STRING", 0x32 << LDAP_OPTNUM_SHIFT, SptString},
	{L"FAST_CONCURRENT_BIND", 0x41 << LDAP_OPTNUM_SHIFT, SptBool},
	{L"GETDSNAME_FLAGS", 0x3d << LDAP_OPTNUM_SHIFT | SDE_OPT_SET, SptMultiEnumOr, &DsGetnameFlagsEnum},
	{L"HOST_NAME", 0x30 << LDAP_OPTNUM_SHIFT, SptString},
	{L"HOST_REACHABLE", 0x3E << LDAP_OPTNUM_SHIFT, SptBool},
	{L"IO_FN_PTRS", 0x0B << LDAP_OPTNUM_SHIFT},
	{L"PING_KEEP_ALIVE", 0x36 << LDAP_OPTNUM_SHIFT, SptInt32},
	{L"PING_LIMIT", 0x38 << LDAP_OPTNUM_SHIFT, SptInt32},
	{L"PING_WAIT_TIME", 0x37 << LDAP_OPTNUM_SHIFT, SptInt32},
	{L"PROMPT_CREDENTIALS", 0x3F << LDAP_OPTNUM_SHIFT, SptBool},
	{L"PROTOCOL_VERSION", 0x11 << LDAP_OPTNUM_SHIFT | SDE_OPT_SET, SptInt32, (PVOID)LDAP_VERSION3},
	{L"VERSION", 0x11 << LDAP_OPTNUM_SHIFT, SptInt32},
	{L"REBIND_ARG", 0x07 << LDAP_OPTNUM_SHIFT},
	{L"REBIND_FN", 0x06 << LDAP_OPTNUM_SHIFT},
	{L"REF_DEREF_CONN_PER_MSG", 0x94 << LDAP_OPTNUM_SHIFT, SptBool},
	{L"REFERRAL_CALLBACK", 0x70 << LDAP_OPTNUM_SHIFT},
	{L"REFERRAL_HOP_LIMIT", 0x10 << LDAP_OPTNUM_SHIFT},
	{L"REFERRALS", 0x08 << LDAP_OPTNUM_SHIFT | SDE_OPT_SET, SptEnum, &ReferralsModeEnum},
	{L"RESTART", 0x09 << LDAP_OPTNUM_SHIFT},
	{L"ROOTDSE_CACHE", 0x9A << LDAP_OPTNUM_SHIFT, SptBool},
	{L"SASL_METHOD", 0x97 << LDAP_OPTNUM_SHIFT, SptString},
	{L"SECURITY_CONTEXT", 0x99 << LDAP_OPTNUM_SHIFT},
	{L"SEND_TIMEOUT", 0x42 << LDAP_OPTNUM_SHIFT, SptInt32},
	{L"SERVER_CERTIFICATE", 0x81 << LDAP_OPTNUM_SHIFT},
	{L"SERVER_ERROR", 0x33 << LDAP_OPTNUM_SHIFT, SptInt32},
	{L"SERVER_EXT_ERROR", 0x34 << LDAP_OPTNUM_SHIFT, SptString},
	{L"SIGN", 0x95 << LDAP_OPTNUM_SHIFT, SptBool},
	{L"SIZELIMIT", 0x03 << LDAP_OPTNUM_SHIFT, SptInt32},
	{L"SSL", 0x0A << LDAP_OPTNUM_SHIFT, SptBool},
	{L"SSL_INFO", 0x93 << LDAP_OPTNUM_SHIFT},
	{L"SSPI_FLAGS", 0x92 << LDAP_OPTNUM_SHIFT | SDE_OPT_SET, SptMultiEnumOr, &SspiFlagEnum},
	{L"TCP_KEEPALIVE", 0x40 << LDAP_OPTNUM_SHIFT, SptBool},
	{L"THREAD_FN_PTRS", 0x05 << LDAP_OPTNUM_SHIFT},
	{L"TIMELIMIT", 0x04 << LDAP_OPTNUM_SHIFT, SptInt32}
};

SDE_OPTS StaticLdapOpts =
{
	RTL_NUMBER_OF(StaticLdapOptions),
	StaticLdapOptions
};

}//namespace Ldap